package com.bh.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.base.domain.JcKyRegistration;
import com.bh.base.mapper.JcRegistrationMapper;
import com.bh.base.service.IJcRegistrationService;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 注册机构Service业务层处理
 * 
 * @author slx
 * @date 2023-11-12
 */
@Service
public class JcRegistrationServiceImpl extends ServiceImpl<JcRegistrationMapper, JcKyRegistration> implements IJcRegistrationService
{
    @Autowired
    private JcRegistrationMapper jcRegistrationMapper;

    /**
     * 查询注册机构
     * 
     * @param id 注册机构主键
     * @return 注册机构
     */
    @Override
    public JcKyRegistration selectJcRegistrationById(Long id)
    {
        return jcRegistrationMapper.selectJcRegistrationById(id);
    }

    /**
     * 查询注册机构列表
     * 
     * @param jcRegistration 注册机构
     * @return 注册机构
     */
    @Override
    public List<JcKyRegistration> selectJcRegistrationList(JcKyRegistration jcRegistration)
    {
        return jcRegistrationMapper.selectJcRegistrationList(jcRegistration);
    }

    /**
     * 新增注册机构
     * 
     * @param jcRegistration 注册机构
     * @return 结果
     */
    @Override
    public int insertJcRegistration(JcKyRegistration jcRegistration)
    {
        if (jcRegistrationMapper.selectCodeUnique(jcRegistration.getCode()) != null) {
            throw new ServiceException("注册机构编码已存在");
        }
        jcRegistration.setCreateName(SecurityUtils.getUsername());
        jcRegistration.setCreateTime(DateUtils.getNowDate());
        return jcRegistrationMapper.insert(jcRegistration);
    }

    /**
     * 修改注册机构
     * 
     * @param jcRegistration 注册机构
     * @return 结果
     */
    @Override
    public int updateJcRegistration(JcKyRegistration jcRegistration)
    {
        JcKyRegistration registration = jcRegistrationMapper.selectCodeUnique(jcRegistration.getCode());
        if (registration != null && !registration.getId().equals(jcRegistration.getId())) {
            throw new ServiceException("注册机构编码已存在");
        }
        jcRegistration.setModifyName(SecurityUtils.getUsername());
        jcRegistration.setModifyTime(DateUtils.getNowDate());
        return jcRegistrationMapper.updateById(jcRegistration);
    }

    /**
     * 批量删除注册机构
     * 
     * @param ids 需要删除的注册机构主键
     * @return 结果
     */
    @Override
    public int deleteJcRegistrationByIds(Long[] ids)
    {
        return jcRegistrationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除注册机构信息
     * 
     * @param id 注册机构主键
     * @return 结果
     */
    @Override
    public int deleteJcRegistrationById(Long id)
    {
        return jcRegistrationMapper.deleteById(id);
    }
}
